home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.02 Feb 96 / Adding Scripts to Menus / Script Menu test project / Powering Up Code / SCApp.cp next >
Encoding:
Text File  |  1995-06-15  |  2.9 KB  |  141 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. // SCApp.cp
  3. // ===========================================================================
  4. // © 1995 James Kaput, Jeremy Roschelle SimCalc Project
  5.  
  6. #include "SCApp.h"
  7. #include "SCDoc.h"
  8. #include "SCModelProperty.h"
  9. #include "SCFinderUtility.h"
  10. #include "SCScriptsMenu.h"
  11.  
  12. long SCApp::sUniqueID = 100;
  13. SCSetPropertyAction    *SCApp::sAction = nil;
  14.  
  15. const ResIDT kScriptsMenuID = 140;
  16.  
  17. void main(void)
  18. {
  19.                                     // Set Debugging options
  20. #ifdef Debug_Throw
  21.     gDebugThrow = debugAction_SourceDebugger; // or debugAction_SourceDebugger
  22. #endif
  23.  
  24. #ifdef Debug_Signal
  25.     gDebugSignal = debugAction_SourceDebugger;
  26. #endif
  27.  
  28.     InitializeHeap(4);
  29.     UQDGlobals::InitializeToolbox(&qd);
  30.             
  31.     SCApp    theApp;
  32.     theApp.Run();
  33. }
  34.  
  35. SCApp::SCApp()
  36. {
  37.     // get spec for the Scripts Folder
  38.     FSSpec    appSpec;
  39.     long        folderID;
  40.     
  41.     UFinder::GetAppSpec(appSpec);    
  42.     folderID = UFinder::GetFolderID(appSpec,"\pScript Menu Items");
  43.         
  44.     // attach a new handler for the scripts menu
  45.     AddAttachment(new SCScriptsMenuHandler(kScriptsMenuID,appSpec.vRefNum,folderID));
  46. }
  47.  
  48.  
  49. void
  50. SCApp::StartUp()
  51. {    
  52.     new SCDoc(this);
  53. }
  54.  
  55. long 
  56. SCApp::GenerateUniqueID()
  57. {
  58.     return ++sUniqueID;
  59. }
  60.  
  61. // note: we can't use LSemanticUndoer for its PostAction, ObeyCommand, and FindCommandStatus
  62. // because LSemanticUndoer has no provision for getting an Undo string from any place but a
  63. // STR# resource, and we are constructing strings from the 'aete' properties.
  64. // this code below is mostly copied from LSemanticUndoer, but changed to ease this restriction
  65.  
  66. // in a real application, you would define a more general class than SCSetPropertyAction
  67. // and be able to Post all actions of that class
  68.  
  69. void    
  70. SCApp::PostAction(SCSetPropertyAction *inAction)
  71. {
  72.     delete sAction;
  73.     sAction = inAction;
  74.     sAction->Redo();
  75. }
  76.  
  77. Boolean    
  78. SCApp::ObeyCommand(
  79.     CommandT    inCommand,
  80.     void        *ioParam)
  81. {
  82.     switch (inCommand) {
  83.     
  84.         case cmd_Undo:
  85.             Assert_(sAction);
  86.             if (sAction->CanRedo() || sAction->CanUndo()) {
  87.                 if (sAction->CanUndo())
  88.                     sAction->Undo();
  89.                 else
  90.                     sAction->Redo();
  91.             }
  92.             return true;
  93.             break;
  94.             
  95.         default:
  96.             return inherited::ObeyCommand(inCommand, ioParam);
  97.             break;
  98.     }
  99.     return false;
  100. }
  101.  
  102. void    
  103. SCApp::FindCommandStatus(
  104.     CommandT    inCommand,
  105.     Boolean        &outEnabled,
  106.     Boolean        &outUsesMark,
  107.     Char16        &outMark,
  108.     Str255        outName)
  109. {
  110.     switch (inCommand) {
  111.  
  112.         case cmd_Undo:
  113.             outEnabled = false;
  114.             if (sAction) {
  115.                 Str255    prop;
  116.                 if (sAction->CanRedo()) {
  117.                     outEnabled = true;
  118.                     CopyPStr("\pRedo ",outName);
  119.                     sAction->GetDescriptor(prop);
  120.                     ConcatPStr(outName,prop);
  121.                 } else if (sAction->CanUndo()) {
  122.                     outEnabled = true;
  123.                     CopyPStr("\pUndo ",outName);
  124.                     sAction->GetDescriptor(prop);
  125.                     ConcatPStr(outName,prop);
  126.                 }
  127.             }
  128.  
  129.             if (!outEnabled) {
  130.                 GetIndString(outName, STRx_Standards, str_CantUndo);
  131.                 ThrowIfResError_();
  132.             }
  133.             break;
  134.  
  135.         default:
  136.             inherited::FindCommandStatus(inCommand, outEnabled, outUsesMark, outMark, outName);
  137.             break;
  138.  
  139.     }
  140. }
  141.